#!/bin/bash
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
if [ -f /tmp/cfg2html.lock ]; then echo "Lock file /tmp/cfg2html.lock exist"; exit 1; fi
touch /tmp/cfg2html.lock
CFG2HTMLDIR=$LOGDIR/cfg2html
ARCHIVEDIR=$CFG2HTMLDIR/archives
DIR=$CFG2HTMLDIR/`uname -n`
if [ ! -d $ARCHIVEDIR ]
then
	mkdir -m 755 -p $ARCHIVEDIR
else
	if cd $ARCHIVEDIR
	then
		ls -1td | grep $(uname -n) | tail -n +10 | xargs \rm -rf
	else
		echo "Cannot descent to $ARCHIVEDIR, exiting"
		exit 1
	fi
fi 
if [ -d $DIR ]; then mv $DIR $ARCHIVEDIR/$(uname -n).`date +'%Y-%m-%d-%H-%M'`; fi
mkdir -p $DIR
if [ "$(uname)" == "Linux" ]
then
	(cd /; /usr/bin/cfg2html -o $DIR)
else
	(cd /; /var/code/sap/scripts/cfg2html_aix_281.sh -D -o $DIR)
fi
chmod -R 755 $DIR
\rm -f /tmp/cfg2html.lock 2>&1 > /dev/null
